$(document).ready(function() {
	$('#submit_inscription').click(function() {
		$.ajax({
			type: "POST",
			url: "adapter/inscriptionValidation.php",
			data: $('form#inscriptionForm').serialize(),
			beforeSend: function (){
				$("#loading").show();
				$("#submit_btn").hide();
			}
			}).done(function(msg) {
				if (msg.CODE_RETOUR == 0) {
					$("#error").show();
				}
				else {
					window.location.href = "index.php";
				}
		});
	});
	
	$('#submit_eleve').click(function() {
		$.ajax({
			type: "POST",
			url: "adapter/eleveValidation.php",
			data: $('form#inscriptionForm').serialize(),
			beforeSend: function (){
				$("#loading").show();
			}
			}).done(function(msg) {
				$("#loading").hide();
				if (msg.CODE_RETOUR == 0) {
					$("#error").show();
				}
				else {
					window.location.href = "index.php";
				}
		});
	});

    $('#payer').click(function() {
        if($("#id_frais_inscription").val() == "" || $("#id_frais_inscription").val() == 0){
            $("#ation_paiement").val("nouveau_frais");

        }
        else {
            $("#ation_paiement").val("nouveau_paiement");
        }

        $.ajax({
			type: "POST",
			url: "adapter/paiement.php",
			data: $('form#form_frais_inscription').serialize()
		}).done(function(msg) {
			if (msg == 0) {
				console.log(msg);
			}
			else {
				var obj = jQuery.parseJSON(msg);
				console.log(obj);
				$('input#montant_total').val('');
				$('input#montant_a_payer').val('');
				$('input#id_frais_inscription').val('');
				$('input#montant_total').val(obj[0].MONTANT_DU);
				$('span#restant_a_payer').html(obj[0].RESTANT_A_PAYE + '&euro;');
				$('input#id_frais_inscription').val(obj[0].ID_FRAIS_INSCRIPTION);
				$('input#id_parent_frais').val(obj[0].ID_PARENT);
			}
        });
    });
});

function inscrirArabe (idInscription) {
	$.ajax({
		type: "POST",
		url: "adapter/eleveInscription.php",
		data: {"idInscription":idInscription, "action":"inscription_arabe", "etat":"1"},
		beforeSend: function (){
			$("#loading").show();
		}
		}).done(function(msg) {
			$("#loading").hide();
	});
}

function desinscrirArabe (idInscription) {
	$.ajax({
		type: "POST",
		url: "adapter/eleveInscription.php",
		data: {"idInscription":idInscription, "action":"inscription_arabe", "etat":"0"},
		beforeSend: function (){
			$("#loading").show();
		}
		}).done(function(msg) {
			$("#loading").hide();
	});
}

function inscrirCoran (idInscription) {
	$.ajax({
		type: "POST",
		url: "adapter/eleveInscription.php",
		data: {"idInscription":idInscription, "action":"inscription_coran", "etat":"1"},
		beforeSend: function (){
			$("#loading").show();
		}
		}).done(function(msg) {
			$("#loading").hide();
	});
}

function desinscrirCoran (idInscription) {
	$.ajax({
		type: "POST",
		url: "adapter/eleveInscription.php",
		data: {"idInscription":idInscription, "action":"inscription_coran", "etat":"0"},
		beforeSend: function (){
			$("#loading").show();
		}
		}).done(function(msg) {
			$("#loading").hide();
	});
}